package com.duke.shaking.storage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.duke.shaking.pojo.ChatFriend;
import com.duke.shaking.pojo.Message;
import com.duke.shaking.pojo.Order;
import com.sohu.pay.order.OrderInfo;

import android.content.ContentValues;
import android.content.Context;

public class DataManager {
	protected static final String TAG = "DataManager";
	private static DataManager dataManager;
	private static String currUserMobile;
	
	private DatabaseHelper dbHelper;
	private Map<String, ChatFriend> friendsCache;
	
	public static DataManager getShared() {
		return dataManager;
	}
	
	public static void prepareDataManager(Context context, String userMobile) {
		if (dataManager == null || !userMobile.equalsIgnoreCase(currUserMobile)) {
			if (dataManager != null) {
				dataManager.close();
				dataManager = null;
			}
			dataManager = new DataManager(context, userMobile);
			currUserMobile = userMobile;
		}
	}
	
	private void close() {
		dbHelper.close();
	}
	
	public DataManager(Context context, String userMobile) {
		dbHelper = new DatabaseHelper(context, userMobile);
		
		friendsCache = new HashMap<String, ChatFriend>();
	}
	
	public boolean userDataIsEmtpy() {
		return dbHelper.isUserDataEmpty;
	}
	
	public void setUserDataIsNotEmpty() {
		dbHelper.isUserDataEmpty = false;
	}
	
	private ChatFriend queryFriendById(String userId) {
		return dbHelper.queryFriendByUserId(userId);
        
	}
	
	private void setFriendByFriend(ChatFriend src, ChatFriend dst) {
		if (src.getNickname() != null) {
			dst.setNickname(src.getNickname());
		}
		
		if (src.getSex() != null) {
			dst.setSex(src.getSex());
		}
	
		if (src.getAge() != null) {
			dst.setAge(src.getAge());
		}
		
		if (src.getCity1() != null) {
			dst.setCity1(src.getCity1());
		}
		
		if (src.getCity2() != null) {
			dst.setCity2(src.getCity2());
		}
		
		if (src.getBirth() != null) {
			dst.setBirth(src.getBirth());
		}
		
		if (src.getAvatarUrl() != null) {
			dst.setAvatarUrl(src.getAvatarUrl());
		}
		
		if (src.getLevel() != null) {
			dst.setLevel(src.getLevel());
		}
		
		/*if (friend.getTotalMessages() != null) {
			cv.put(MSGCOUNT, friend.getTotalMessages());
		}
		
		if (friend.getUnreadMessages() != null) {
			cv.put(UNREADCOUNT, friend.getUnreadMessages());
		}
		*/
		if (src.isNewFriend() != null) {
			dst.setNewFriend(src.isNewFriend());
		}
		if (src.getLastMsgTime() != null) {
			dst.setLastMsgTime(src.getLastMsgTime());
		}

	}
	
	public ChatFriend getFriendById(String userId) {
		ChatFriend friend = friendsCache.get(userId);
		if (friend != null) {
			return friend;
		}
		friend = queryFriendById(userId);
		if (friend != null) {
			friendsCache.put(friend.getUserId(), friend);
		}
		return friend;
	}
	
	public boolean putOrUpdateFriend(ChatFriend friend) {
		if (dbHelper.putOrUpdateFriend(friend)) {
			ChatFriend old = friendsCache.get(friend.getUserId());
			if (old != null) {
				setFriendByFriend(friend, old);
			}
			return true;
		}
		return false;
	}
	
	public List<ChatFriend> loadFriends() {
		try {
			return dbHelper.loadFriends();
		}
		catch(Exception exp) {
			exp.printStackTrace();
		}
		return null;
	}
	
	public void removeFriendByUserId(String userId) {
		friendsCache.remove(userId);
		dbHelper.removeFriendByUserId(userId);
	}
	
	public void removeAllFriends() {
		friendsCache.clear();
		dbHelper.removeAllFriends();
	}
	
	public List<Message> loadUnreadMessagesByFriendId(String userId, int count) {
		List<Message> msgs = dbHelper.loadMessagesByFriendId(userId, count, true, -1);
		if (msgs != null) {
			setMessagesRead(msgs);
		}
		return msgs;
	}

	public List<Message> loadReadMessagesByFriendId(String userId, int count, long maxMsgId) {
		return dbHelper.loadMessagesByFriendId(userId, count, false, maxMsgId);
	}
	
	// return the msgId, or -1 if a error occurred
	public long putMessage(Message msg) {
		return dbHelper.putMessage(msg);
	}
	
	public void removeMessageById(long msgId) {
		dbHelper.removeMessageById(msgId);
	}
	
	public void removeAllMessagesOfFriendByUserId(String userId) {
		dbHelper.removeAllMessagesOfFriendByUserId(userId);
	}
	
	public void removeAllMessages() {
		dbHelper.removeAllMessages();
	}
	
	public void setMessageReadById(long msgId) {
		dbHelper.setMessageReadById(msgId);
	}
	
	public int setMessagesRead(List<Message> msgs) {
		int count = 0;
		for (Message msg : msgs) {
			if (dbHelper.setMessageReadById(msg.getMsgId())) {
				count ++;
			}
		}
		return count;
	}
	
	public void setMessageSendStateById(long msgId, int sendState) {
		dbHelper.setMessageSendStateById(msgId, sendState);
	}
	
	public int getTotalUnreadMsgCount() {
		return dbHelper.getTotalUnreadMsgCount();
	}
	
	public long putOrder(Order order) {
		
		return dbHelper.putOrder(order);
	}
	
	public void removeOrder(long orderId) {
		dbHelper.removeOrder(orderId);
	}
	
	public boolean updateOrderState(Order order) {
		
		return (dbHelper.updateOrderState(order) > 0);
	}
	
	public List<Order> loadUnfinishOrder() {
		return dbHelper.loadOrder(true);
	}
	
	public List<Order> loadAllOrder() {
		return dbHelper.loadOrder(false);
	}
}
